/**
 * 
 */
package com.cqan.account.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cqan.entity.pk.LongEntity;

/**
 * @Description 权限
 * @author slaton.wu@gmail.com
 * @date 2012-6-29下午6:49:39
 * 
 */
@Entity
// 表名与类名不相同时重新定义表名.
@Table(name = "TBL_AUTH")
// 默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Authority extends LongEntity {

	public enum AuthType {

		MENU, OPERATION, OTHER;

		public String getLabel() {
			switch (this) {
			case MENU:
				return "菜单";
			case OPERATION:
				return "操作";
			case OTHER:
				return "其它";
			}
			return "未知";
		}
	}

	private AuthType authType;

	private List<Authority> children;

	private String code;

	private Date cteateTime;

	private boolean enabled;
	
	private String name;
	
	private Authority parent;

	private Date updateTime;
	
	private String url;
	
	@Column(name = "UPDATE_TIME")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Column(name = "AUTH_TYPE")
	@Enumerated(EnumType.STRING)
	public AuthType getAuthType() {
		return authType;
	}

	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REFRESH}, fetch = FetchType.LAZY, targetEntity = Authority.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Authority> getChildren() {
		return children;
	}

	@Column(name = "CODE")
	public String getCode() {
		return code;
	}

	@Column(name = "CREATE_TIME")
	public Date getCteateTime() {
		return cteateTime;
	}

	@Column(name = "NAME")
	public String getName() {
		return name;
	}

	@ManyToOne
	@JoinColumn(name="PID")
	public Authority getParent() {
		return parent;
	}

	public String getUrl() {
		return url;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setAuthType(AuthType authType) {
		this.authType = authType;
	}

	public void setChildren(List<Authority> children) {
		this.children = children;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setCteateTime(Date cteateTime) {
		this.cteateTime = cteateTime;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParent(Authority parent) {
		this.parent = parent;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
